/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package networkrespnse;

import core.ParameterDecoder;
import core.Settings;
import utils.Utils;

/**
 *
 * @author Kvest
 */
public class RefusePreliminaryOrderAnswer extends Response {
    public int orderId;
    public String signed;
    public boolean isDriverSigned;
            
    public RefusePreliminaryOrderAnswer(byte[] data)
    {
        super(REFUSE_PRELIMINARY_ORDER_ANSWER);
        orderId = -1;
        signed = "";
        isDriverSigned = false;
        
        parse(data);
    }
    
    protected void parse(byte[] data)
    {
        OrderResponse orderResp;
        byte[] body,temp4, temp;
        int index,size;
        index = 0;
        body = ParameterDecoder.getTransferBody(data);

        temp4 = new byte[4];
        for (int i=0;i<4;i++)
        {
            temp4[i] = body[index++];
        }
        int offset = Utils.byteToInt(temp4);
        index += offset;

        //OrderId
        for (int i=0;i<4;i++)
        {
            temp4[i] = body[index++];
        }
        orderId = Utils.byteToInt(temp4);
        
        //CallSigns
        String nick = Settings.getSettings().getNick();
        String nextSigned;
        for (int i=0;i<4;i++)
        {
            temp4[i] = body[index++];
        }
        int count = Utils.byteToInt(temp4);
        for (int j = 0; j < count; ++j) {
            for (int i=0;i<4;i++)
            {
                temp4[i] = body[index++];
            }
            int strSize = Utils.byteToInt(temp4);
            temp = new byte[strSize];
            for(int i=0;i<strSize;i++)
            {
                temp[i] = body[index++];
            }
            nextSigned = Utils.byteToString(temp);
            if (nick.equals(nextSigned)) {
                isDriverSigned = true;
            }
            signed += nextSigned;
            if (j != (count - 1))
            {
                signed += ",";
            }
        }     
    }
}
